package com.tianze.library.utils;

/**
 * 坐标转换
 * 一个提供了百度坐标（BD09）、国测局坐标（火星坐标，GCJ02）、和WGS84坐标系之间的转换的工具类
 */
public class GpsUtils {

    private static double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    private static double PI = 3.1415926535897932384626;
    private static double a = 6378245.0;
    private static double ee = 0.00669342162296594323;

    private GpsUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
     * 即 百度 转 谷歌、高德
     *
     * @param lon
     * @param lat
     * @return Double[lon, lat]
     */
    public static Double[] BD09ToGCJ02(Double lon, Double lat) {
        double x = lon - 0.0065;
        double y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
        Double[] arr = new Double[2];
        arr[0] = z * Math.cos(theta);
        arr[1] = z * Math.sin(theta);
        return arr;
    }

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
     * 即谷歌、高德 转 百度
     *
     * @param lon
     * @param lat
     * @return Double[lon, lat]
     */
    public static Double[] GCJ02ToBD09(Double lon, Double lat) {
        double z = Math.sqrt(lon * lon + lat * lat) + 0.00002 * Math
                .sin(lat * x_PI);
        double theta = Math.atan2(lat, lon) + 0.000003 * Math.cos(lon * x_PI);
        Double[] arr = new Double[2];
        arr[0] = z * Math.cos(theta) + 0.0065;
        arr[1] = z * Math.sin(theta) + 0.006;
        return arr;
    }

    /**
     * WGS84转GCJ02
     *
     * @param lon
     * @param lat
     * @return Double[lon, lat]
     */
    public static Double[] WGS84ToGCJ02(Double lon, Double lat) {
        if (outOfChina(lon, lat)) {
            return new Double[]{lon, lat};
        }
        double dlat = transformlat(lon - 105.0, lat - 35.0);
        double dlng = transformlng(lon - 105.0, lat - 35.0);
        double radlat = lat / 180.0 * PI;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
        Double[] arr = new Double[2];
        arr[0] = lon + dlng;
        arr[1] = lat + dlat;
        return arr;
    }

    /**
     * GCJ02转WGS84
     *
     * @param lon
     * @param lat
     * @return Double[lon, lat]
     */
    public static Double[] GCJ02ToWGS84(Double lon, Double lat) {
        if (outOfChina(lon, lat)) {
            return new Double[]{lon, lat};
        }
        double dlat = transformlat(lon - 105.0, lat - 35.0);
        double dlng = transformlng(lon - 105.0, lat - 35.0);
        double radlat = lat / 180.0 * PI;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
        double mglat = lat + dlat;
        double mglng = lon + dlng;
        return new Double[]{lon * 2 - mglng, lat * 2 - mglat};
    }

    private static Double transformlat(double lon, double lat) {
        double ret = -100.0 + 2.0 * lon + 3.0 * lat + 0.2 * lat * lat + 0.1 * lon * lat + 0.2 * Math
                .sqrt(Math.abs(lon));
        ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static Double transformlng(double lon, double lat) {
        double ret = 300.0 + lon + 2.0 * lat + 0.1 * lon * lon + 0.1 * lon * lat + 0.1 * Math
                .sqrt(Math.abs(lon));
        ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lon * PI) + 40.0 * Math.sin(lon / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lon / 12.0 * PI) + 300.0 * Math.sin(lon / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * outOfChina
     *
     * @param lon
     * @param lat
     * @return {boolean}
     * @描述: 判断是否在国内，不在国内则不做偏移
     */
    private static boolean outOfChina(Double lon, Double lat) {
        return (lon < 72.004 || lon > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
    }

}
